package me.numark.view;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SectionIndexer;
import android.widget.TextView;

import java.util.ArrayList;

import me.numark.R;


public abstract class ContactsCursorBaseAdapter extends SectionedCursorAdapter implements SectionIndexer {
    public Context mContext;
    ArrayList<Section> sections;
    public LayoutInflater mLayoutInflater;

    public static class Section {
        public int count;
        public int start;
        public String header;
        public char firstChar;
    }

    public ContactsCursorBaseAdapter(Context context, Cursor cursor) {
        super(context, cursor);
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void updateData(Cursor cursor, ArrayList<Section> sections) {
        this.sections = sections;
        swapCursor(cursor);
    }


    public void clear() {
        this.sections = new ArrayList<Section>();
        swapCursor(null);
    }

    @Override
    public Object getItem(int section, int position) {
        Section s = sections.get(section);
        int pos = s.start + position;
        Cursor c = super.getCursor();
        c.moveToPosition(pos);
        return c;
    }

    @Override
    public long getItemId(int section, int position) {
        if (sections == null || sections.size() == 0) {
            return position;
        }

        Section s = sections.get(section);
        return s.start + position;
    }

    @Override
    public int getNormalPosition(int section, int position) {
        if (sections == null || sections.size() == 0) {
            return position;
        }

        Section s = sections.get(section);
        return s.start + position;
    }

    @Override
    public int getSectionCount() {
        if (sections == null) {
            return 0;
        }
        return sections.size();
    }

    @Override
    public int getCountForSection(int section) {
        if (sections == null) {
            return 0;
        }
        return sections.get(section).count;
    }

    @Override
    public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
        TextView tv = null;

        if (convertView instanceof TextView) {
            tv = (TextView) convertView;
        }

        if (tv == null) {
            tv = (TextView) mLayoutInflater.inflate(R.layout.section_header_item, parent, false);
        }
        Section sec = sections.get(section);
        tv.setText(sec.header);
        return tv;
    }

    @Override
    public Object[] getSections() {
        if (sections == null) {
            return new String[0];
        }
        String[] headers = new String[sections.size()];
        for (int i = 0; i < sections.size(); i++) {
            Section s = sections.get(i);
            headers[i] = s.header;
        }
        return headers;
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        if (sections == null) {
            return 0;
        }
        Section s = sections.get(sectionIndex);
        return s.start + sectionIndex;
    }
}
